///////////////////////////////////////////////////////////////////////////////
//
// lighthouse3D - 3D Software Library
//
// File: CFont.cpp
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2013 Marcus Reinhardt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////
//
// You can find a copy of the GNU General Public License (gpl.txt) in the
// directory of the library.
//
///////////////////////////////////////////////////////////////////////////////
#include "CFont.h"

///////////////////////////////////////////////////////////////////////////////
// CFont::CFont( ... )
///////////////////////////////////////////////////////////////////////////////
CFont::CFont()
{
}

///////////////////////////////////////////////////////////////////////////////
// CFont::~CFont( ... )
///////////////////////////////////////////////////////////////////////////////
CFont::~CFont()
{
}

void CFont::extractLetter(int i, TPictureData *p, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
{
	int x,y,xb,yb;

	currentFont.fontData[i].sizeX = x2-x1;
	currentFont.fontData[i].sizeY = y2-y1-1;
	currentFont.sizeY = y2-y1-1;

	yb = -1;

	for (y=(int)y1+1; y<(int)y2; y++)
	{
		yb++;
		xb=0;

		for (x=(int)x1; x<(int)x2; x++)
		{
			currentFont.fontData[i].rgb[yb*currentFont.fontData[i].sizeX+xb] = p->rgb[y*p->sizeX+x];
			xb++;
		}
	}
}

bool CFont::loadFont(char *fontName)
{
	unsigned int sc, ec, tc, s;
	int nB, nE;
	int x,y,i;

	if (fileHandler.loadBMPToPicture(fontName, &tmpPic) == false)
		return false;

	sc = tmpPic.rgb[0*tmpPic.sizeX+0];
	ec = tmpPic.rgb[0*tmpPic.sizeX+1];
	tc = tmpPic.rgb[0*tmpPic.sizeX+2];

	currentFont.transparentColor = tc;
	currentFont.startColor = sc;
	currentFont.endColor = ec;
	currentFont.sizeY = 0;

	s = 3;
	nB=1;
	nE=0;
	// Parse for begin and end position of each letter
	for (y=0; y<tmpPic.sizeY; y++)
	{
		for (x=s; x<tmpPic.sizeX; x++)
		{
			s = 0;

			if ( tmpPic.rgb[y*tmpPic.sizeX+x] == sc )
			{				

				letterBx[nB] = x;
				letterBy[nB] = y;

				nB++;

				if (nB > 512)
					return false;
			}
			if ( tmpPic.rgb[y*tmpPic.sizeX+x] == ec )
			{				

				letterEx[nE] = x;
				letterEy[nE] = y;

				nE++;

				if (nE > 512)
					return false;
			}

		}
	}

	if (nE != nB)
		return false;

	// Now copy them to TFontData
	for (i=0; i<nB; i++)
		extractLetter((int) i, &tmpPic, letterBx[i], letterBy[i], letterEx[i], letterEy[i]);

	currentFont.fontOffset = 32;

	return true;
}

TFontLetter *CFont::getLetterData(int i)
{
	return &( currentFont.fontData[i-currentFont.fontOffset] );
}

unsigned int CFont::getTransparentColor()
{
	return currentFont.transparentColor;
}

unsigned int CFont::getOffset()
{
	return currentFont.fontOffset;
}
